/**
 * JAVACC DEMO 1.0
 */
package com.apache.audit.controller;

import com.apache.action.ActionSupport;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.audit.entity.LogOperation;
import com.apache.audit.entity.LogOperationDetail;
import com.apache.audit.entity.LogOperationDetailVo;
import com.apache.audit.manager.LogOperationDetailManager;
import com.apache.audit.manager.LogOperationManager;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.uct.common.PBOSSOTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  审记操作记录action
 *
 */
@Controller
@RequestMapping("/audit/logger/")
public class LogOperationAction extends ActionSupport<LogOperation> {

    @Autowired
    private LogOperationManager logOperationManager;

    @Autowired
    private LogOperationDetailManager logOperationDetailManager;

    /**
     * 功能说明：通用跳转到列表查询页面
     */
    @RequestMapping("list.action")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(LIST);
        if (!"1".equals(getSysFlag(request))) {
            view.addObject("orgId", getOrgId(request));
        }
        return view;
    }

    /**
     * description:  日志操作列表
     */
    @RequestMapping("data.action")
    @ResponseBody
    protected Object data(HttpServletRequest request, String page, String rows) {
        ParamsVo<LogOperation> vo = new ParamsVo<LogOperation>();
        LogOperation model = new LogOperation();
        if (Validator.isNotNull(request.getParameter("createUser"))) {
            model.setCreateUser(request.getParameter("createUser"));
        }
        if (Validator.isNotNull(request.getParameter("modelSysname"))) {
            vo.setParams("modelSysname", request.getParameter("modelSysname"));
        }
        if (Validator.isNotNull(request.getParameter("modelBussiness"))) {
            vo.setParams("modelBussiness", request.getParameter("modelBussiness"));
        }
        if (Validator.isNotNull(request.getParameter("modelCname"))) {
            vo.setParams("modelCname", request.getParameter("modelCname"));
        }
        if (Validator.isNotNull(request.getParameter("logType"))) {
            model.setLogType(request.getParameter("logType"));
        }
        if (Validator.isNotNull(request.getParameter("createdTime"))) {//格式2015-04-21
            vo.setParams("createdTime", request.getParameter("createdTime"));
        }
        if (Validator.isNotNull(request.getParameter("orgId"))) {
            model.setOrgId(request.getParameter("orgId"));
        }
        vo.setParams("pageIndex", page);
        vo.setParams("pageSize", rows);
        vo.setObj(model);
        Page pageList = logOperationManager.getPageInfo(vo);
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("total", pageList.getCount());
        mapData.put("rows", pageList.getPageObjects());
        return mapData;
    }

    /**
     * TODO 查看日志详细.
     */
    @RequestMapping(value = "/view.action")
    public ModelAndView view(Model model, String id) {
        ModelAndView view = new ModelAndView("audit/log-operation-view");
        view.addObject("logId", id);
        view.addObject("item", getLogOpationInfo(id));
        return view;
    }

    /**
     * description:  日志操作详细列表
     */
    @RequestMapping("detail!data.action")
    @ResponseBody
    protected Object dataDeatil(HttpServletRequest request, String page, String rows) {
        String logId = request.getParameter("logId");
        LogOperation logopation = getLogOpationInfo(logId);
        if (Validator.isNotNull(logId)) {
            ParamsVo<LogOperationDetail> vo = new ParamsVo<LogOperationDetail>();
            LogOperationDetail model = new LogOperationDetail();
            model.setLogId(logId);
            vo.setObj(model);
            List<LogOperationDetail> list = logOperationDetailManager.getList(vo);
            List<LogOperationDetailVo> result = new ArrayList<LogOperationDetailVo>();
            if (!Validator.isEmpty(list)) {
                List<LogOperationDetail> oldlist = null;
                LogOperation oldLog = getOldLogOperation(logopation);
                int oldCount = 0;
                if (!Validator.isEmpty(oldLog)) {
                    model.setLogId(oldLog.getLogId());
                    vo.setObj(model);
                    oldlist = logOperationDetailManager.getList(vo);
                    oldCount = oldlist.size();
                }
                for (int i = 0; i < list.size(); i++) {
                    LogOperationDetailVo logVo = new LogOperationDetailVo(list.get(i));
                    if ("更新".equals(logopation.getLogType())) {
                        if (oldCount == 0) {
                            logVo.setOldValue(logVo.getPropertyValue());
                        } else {
                            for (int j = 0; j < oldCount; j++) {
                                LogOperationDetail detail = oldlist.get(j);
                                if (logVo.getPropertyEname().equals(detail.getPropertyEname())) {
                                    logVo.setOldValue(detail.getPropertyValue());
                                    break;
                                }
                            }
                        }
                    }
                    result.add(logVo);
                }
            }
            return result;
        }
        return null;
    }

    private LogOperation getOldLogOperation(LogOperation logopation) {
        if (Validator.isNull(logopation.getPrimaryKey()))
            return null;
        ParamsVo<LogOperation> ovo = new ParamsVo<LogOperation>();
        LogOperation logo = new LogOperation();
        logo.setPrimaryKey(logopation.getPrimaryKey());
        logo.setModelId(logopation.getModelId());
        logo.setCreatedTime(logopation.getCreatedTime());
        ovo.setParams("pageIndex", "1");
        ovo.setParams("pageSize", "2");
        ovo.setObj(logo);
        Page pageList = logOperationManager.getPageInfo(ovo);
        List olist = pageList.getPageObjects();
        if (olist.size() > 1) {
            return (LogOperation) olist.get(1);
        }
        return null;
    }

    private LogOperation getLogOpationInfo(String id) {
        ParamsVo vo = new ParamsVo();
        vo.setInfoId(id);
        LogOperation obj = (LogOperation) getBaseManager().getInfoById(vo);
//        System.out.println(obj.getLogId());
        return obj;
    }

    protected BaseManager<LogOperation> getBaseManager() {
        return logOperationManager;
    }

    protected String getPrefix() {
        return "audit/log-operation-";
    }

    private String getSysFlag(HttpServletRequest request) {
        return Validator
                .getDefaultStr(String.valueOf(request.getSession().getAttribute("sysUser")), "0");
    }

    private String getOrgId(HttpServletRequest request) {
        if (!Validator.isEmpty(request.getSession().getAttribute("loginUser"))) {
            return PBOSSOTools.getLoginUser(request).getOrgId();
        }
        return "";
    }

}
